<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="js/getRequest.js"></script>
    <title>Title</title>
<script>
    function send() {
        //发送Ajax请求

        //获取xmlHttpRequest对象
        var xmlhttp = getRequest();

        //监听请求响应状态
        xmlhttp.onreadystatechange = function () {
            alert(xmlhttp.readyState);
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(this.responseText);//获取响应数据
            }
        }

        // get发送ajax请求
        xmlhttp.open("GET", "${pageContext.request.contextPath}/servletDemo1", true);
        xmlhttp.send();// 参数只有是post请求才写参数

        // post发送ajax请求
        xmlhttp.open("post", "${pageContext.request.contextPath}/servletDemo1", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("username=tom");// 参数只有是post请求才写参数
    }
</script>
</head>
<body>
<input type="button" value="发送Ajax" onclick="send()">
</body>
</html>
